package com.sfh.cpc;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sfh.cpc.com.entity.RspPage;
import com.sfh.cpc.com.refresh.SwipeRefreshMoreLayout;
import com.sfh.lib.mvvm.service.BaseViewModel;
import com.sfh.lib.ui.AbstractLifecycleFragment;

/**
 * 功能描述:刷新分页
 *
 * @author SunFeihu 孙飞虎
 * @date 2019/6/12
 */
public abstract class BaseSwipeRefreshMoreLayoutFragment<VM extends BaseViewModel> extends AbstractLifecycleFragment<VM> implements SwipeRefreshMoreLayout.OnLoadingListener, BaseQuickAdapter.OnItemClickListener {


    public abstract BaseQuickAdapter getAdapter();

    public abstract void loadPage(int nowPage);

    public SwipeRefreshMoreLayout swipeRefreshMoreLayout;

    public int nowPage = 1;

    public boolean onAutoRefresh = true;
    @Override
    public int getLayout() {
        return R.layout.comm_swipe_refresh_more_layout;
    }

    @Override
    public void initData(View view) {
        swipeRefreshMoreLayout = view.findViewById(R.id.swipe_refresh_more_layout);
        swipeRefreshMoreLayout.setAdapter(this.getAdapter(), true);
        swipeRefreshMoreLayout.setOnLoadingListener(this);
        swipeRefreshMoreLayout.setOnItemClickListener(this);
        if (onAutoRefresh){
            swipeRefreshMoreLayout.onAutoRefresh();
        }

    }

    public  void onPageData(RspPage data) {

        if (data == null){
            this.swipeRefreshMoreLayout.onLoadMoreFail();
        }
        this.nowPage = data.getPageNumber();
        this.swipeRefreshMoreLayout.onRefreshCompale(data.getList(), data.getPageNumber() != 1);
        if (data.getTotalPage() == data.getPageNumber()) {
            this.swipeRefreshMoreLayout.onLoadAllCompale();
        }
    }

    @Override
    public void onRefreshUI() {
        this.nowPage = 1;
        this.loadPage(this.nowPage);
    }

    @Override
    public void onLoadMoreUI() {
        this.nowPage++;
        this.loadPage(this.nowPage);
    }
}
